package com.fbee.smartcommunities.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fbee.smartcommunities.entity.Role;
import com.fbee.smartcommunities.mapper.RoleMapper;
import com.fbee.smartcommunities.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.minio.errors.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 调整AUTO_INCREMENT值 服务实现类
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-16
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public List<Role> getRoles(Integer id) {
        if(id == null){
            return list();
        } else {
            List<Role> list = new ArrayList<>();
            list.add(getById(id));
            return list;
        }
    }

    @Override
    public Page<Role> getTargetPage(Integer pageNo, Integer pageSize, String keyword) {
        Page<Role> page = new Page<>(pageNo,pageSize);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",keyword);
        return roleMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Boolean check(Long id, String value, String column) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        Long count;
        if(id != null){
            count = count(queryWrapper.eq(column, value).ne("id",id));
        } else {
            count = count(queryWrapper.eq(column, value));
        }
        // 大于零返回false 等于零返回true
        return !(count > 0);
    }

    @Override
    public Boolean update(Long id, String name, String description) {
        UpdateWrapper<Role> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        if(name != null){
            updateWrapper.set("name",name);
        }
        if(description != null){
            updateWrapper.set("description",description);
        }
        return roleMapper.update(null,updateWrapper) > 0;
    }
}
